package com.example.newsapp.view.activity;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.newsapp.R;

public class VideoPlayActivity extends AppCompatActivity {
    private VideoView videoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_play);

        String videoUrl = getIntent().getStringExtra("videoUrl");
        String title = getIntent().getStringExtra("title");

        // 设置标题
        setTitle(title);

        videoView = findViewById(R.id.videoView);
        // 设置媒体控制器
        MediaController mediaController = new MediaController(this);
        videoView.setMediaController(mediaController);

        // 设置视频路径
        videoView.setVideoURI(Uri.parse(videoUrl));

        // 准备完成后自动播放
        videoView.setOnPreparedListener(MediaPlayer::start);

        // 开始加载
        videoView.requestFocus();
        videoView.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (videoView.isPlaying()) {
            videoView.pause();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        videoView.stopPlayback();
    }
}